<?php

/**
 * @Author: xuzhiyi
 * @Date:   2016-02-23 00:06:06
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-03-03 19:59:17
 * ------------------------------------------------ *
 * Articles                                          *
 * ------------------------------------------------ */
namespace Home\Model;
class CartModel extends CommonModel {
	protected $tableName = 'goods'; 
	

//添加商品到购物车

	public function addToCart(){
		//判断一下该商品是否正常出售
		$goodsInfo = $this->getGoodsInfo((int)I("goodsId"),(int)I("goodsAttrId",0));
		if($goodsInfo['goodsId']=='')return array();
		$goodsInfo["cnt"] = ((int)I("gcount")>0)?(int)I("gcount"):1;
		$goodsInfo["ischk"] = 1;
		$cartgoods = array();
		$totalMoney = 0;
		$cart = session("cart");
		//如果 购物车为空则放入购物车中
		if(empty($cart)){
			$shopcat = array();
			$shopcat[$goodsInfo["goodsId"]."_".$goodsInfo["goodsAttrId"]] = $goodsInfo;
			$totalMoney += $goodsInfo["cnt"]*$goodsInfo["shopPrice"];
			$cartgoods[] = $goodsInfo;		
			session("cart",$shopcat);
		}else{
			//如果购物车不为空则要看下该商品是否原来就有。
			$shopcat = $cart;
			//如果已经存在则要把数量相加
			if(!empty($shopcat[$goodsInfo['goodsId']."_".$goodsInfo["goodsAttrId"]])){
				$goodsInfo["cnt"]=$cart[$goodsInfo["goodsId"]."_".$goodsInfo["goodsAttrId"]]["cnt"]+$goodsInfo["cnt"];
			}			
			$shopcat[$goodsInfo["goodsId"]."_".$goodsInfo["goodsAttrId"]] = $goodsInfo;				
            //重新把购物车内的数据拿到外边
			foreach($shopcat as $key=>$cgoods){	
				$totalMoney += $cgoods["cnt"]*$cgoods["shopPrice"];
				$cartgoods[] = $cgoods;
			}		
			session("cart",$shopcat);
		}
		$cartInfo = array();
		$cartInfo["cartgoods"] = $cartgoods;
		$cartInfo["totalMoney"] = $totalMoney;
		return $cartInfo;
	}
	

//获取商品信息

	public function getGoodsInfo($goodsId,$goodsAttrId = 0){
		$sql = "SELECT g.attrCatId,g.goodsId,g.goodsSn,g.goodsName,g.goodsImg,g.shopId,g.marketPrice,g.shopPrice,g.goodsStock,sp.shopName
				FROM __PREFIX__goods g ,__PREFIX__shops sp WHERE g.shopId=sp.shopId AND goodsFlag=1 and isSale=1 and goodsStatus=1 and g.goodsId = $goodsId";
		$goodslist = $this->query($sql);
		//如果商品有价格属性的话则获取其价格属性
		if(!empty($goodslist) && $goodslist[0]['attrCatId']>0){
			$sql = "select ga.id,ga.attrPrice,ga.attrStock,a.attrName,ga.attrVal,ga.attrId from __PREFIX__attributes a,__PREFIX__goods_attributes ga
			        where a.attrId=ga.attrId and a.catId=".$goodslist[0]['attrCatId']." and a.isPriceAttr=1 
			        and ga.goodsId=".$goodslist[0]['goodsId']." and id=".$goodsAttrId;
			
			$priceAttrs = $this->query($sql);
			if(!empty($priceAttrs)){
				$goodslist[0]['attrId'] = $priceAttrs[0]['attrId'];
				$goodslist[0]['goodsAttrId'] = $priceAttrs[0]['id'];
				$goodslist[0]['attrName'] = $priceAttrs[0]['attrName'];
				$goodslist[0]['attrVal'] = $priceAttrs[0]['attrVal'];
				$goodslist[0]['shopPrice'] = $priceAttrs[0]['attrPrice'];
				$goodslist[0]['goodsStock'] = $priceAttrs[0]['attrStock'];
			}
		}else{
			$goodslist[0]['goodsAttrId'] = 0;
		}
		return $goodslist[0];
	}
	

//获取购物车信息

	public function getCartInfo(){		
		$totalMoney = 0;
		$shopcart = session("cart")?session("cart"):array();
		$cartgoods = array();
		//dump($shopcart);
		foreach($shopcart as $goodsId=>$cgoods){
			$temp = explode('_',$goodsId);
			$goodsId = (int)$temp[0];
			$goodsAttrId = (int)$temp[1];
			$sql = "SELECT  g.goodsImg,g.goodsId,g.shopPrice,g.goodsName,g.shopId,g.goodsStock,g.shopPrice,shop.shopName,shop.qqNo,shop.deliveryType,shop.shopAtive,
					shop.shopTel,shop.shopAddress,shop.deliveryTime,shop.isInvoice, shop.deliveryStartMoney,
					shop.deliveryFreeMoney,shop.deliveryMoney ,g.goodsSn,shop.serviceStartTime,shop.serviceEndTime
					FROM __PREFIX__goods g, __PREFIX__shops shop
					WHERE g.goodsId = $goodsId AND g.shopId = shop.shopId AND g.goodsFlag = 1 and g.isSale=1 and g.goodsStatus=1 ";
			$goods = $this->query($sql);
			$goods=$goods[0];

		    //如果商品有价格属性的话则获取其价格属性
		    if(!empty($goods) && $cgoods['attrCatId']>0){
			    $sql = "select ga.id,ga.attrPrice,ga.attrStock,a.attrName,ga.attrVal,ga.attrId from __PREFIX__attributes a,__PREFIX__goods_attributes ga
			             where a.attrId=ga.attrId and a.catId=".$cgoods['attrCatId']." and a.isPriceAttr=1
			             and ga.goodsId=".$goodsId." and ga.id=".$goodsAttrId;
			            //echo $sql;

				$priceAttrs = $this->query($sql);
				if(!empty($priceAttrs)){
					$goods['goodsAttrId'] = $priceAttrs[0]['id'];
					$goods['attrName'] = $priceAttrs[0]['attrName'];
					$goods['attrVal'] = $priceAttrs[0]['attrVal'];
					$goods['shopPrice'] = $priceAttrs[0]['attrPrice'];
					$goods['goodsStock'] = $priceAttrs[0]['attrStock'];
				}
			}else{
				$goods['goodsAttrId'] = 0;
			}

			$goods["cnt"] = $cgoods["cnt"];
			$goods["ischk"] = $cgoods["ischk"];
			if($goods["ischk"]==1)$totalMoney += $goods["cnt"]*$goods["shopPrice"];

			if($startTime<$goods["startTime"]){
				$startTime = $goods["startTime"];
			}
			if($endTime>$goods["endTime"]){
				$endTime = $goods["endTime"];
			}
		
			$cartgoods[$goods["shopId"]]["shopgoods"][] = $goods;
			$cartgoods[$goods["shopId"]]["deliveryFreeMoney"] = $goods["deliveryFreeMoney"];//店铺免运费最低金额
			$cartgoods[$goods["shopId"]]["deliveryMoney"] = $goods["deliveryMoney"];//店铺配送费
			$cartgoods[$goods["shopId"]]["deliveryStartMoney"] = $goods["deliveryStartMoney"];//店铺配送费
			$cartgoods[$goods["shopId"]]["totalCnt"] = $cartgoods[$goods["shopId"]]["totalCnt"]+$cgoods["cnt"];
			$cartgoods[$goods["shopId"]]["totalMoney"] = $cartgoods[$goods["shopId"]]["totalMoney"]+(($goods["ischk"]==1)?$goods["cnt"]*$goods["shopPrice"]:0);
		}
		
		//计算运费
		$totalDeliveryMoney=0;
		foreach($catgoods as $key=> $cshop){
			if($cshop["totalMoney"]<$cshop["deliveryFreeMoney"]){
				$totalDeliveryMoney += $cshop["deliveryMoney"];
			}
		}
		/*foreach($catgoods as $key=> $cshop){
			if($cshop["totalMoney"]<$cshop["deliveryFreeMoney"]){
				$totalMoney = $totalMoney + $cshop["deliveryMoney"];
			}
		}*/
		
		$cartInfo = array();
		$cartInfo["totalMoney"] = $totalMoney;
		$cartInfo['totalDeliveryMoney']=$totalDeliveryMoney;
		$cartInfo["totalMoneyAll"] =$totalMoney+$totalDeliveryMoney;
		$cartInfo["cartgoods"] = $cartgoods;
		return $cartInfo;
		
	}
   

//检测购物车中商品库存

	public function checkCatGoodsStock(){
		$shopcart = session("cart")?session("cart"):array();	
		$cartgoods = array();		
		foreach($shopcart as $key=>$cgoods){
			$temp = explode('_',$key);
			$goodsId = (int)$temp[0];
			$goodsAttrId = (int)$temp[1];
			$obj = array();
			$obj["goodsId"] = $goodsId;
			$obj["goodsAttrId"] = $goodsAttrId;
			$goods = $this->getGoodsStock($obj);
			$goods['goodsAttrId'] = $goodsAttrId;
			$goods["cnt"] = $cgoods["cnt"];
			$goods["stockStatus"] = ($goods["goodsStock"]>=$goods["cnt"])?1:0;		
			$cartgoods[] = $goods;
		}
		
		return $cartgoods;
		
	}
	

//删除购物车中的商品

	public function delCartGoods(){
		$goodsKey = (int)I("goodsId")."_".(int)I("goodsAttrId",0);
		$shopcart = session("cart")?session("cart"):array();
		session("cart");
		$newShopcat = array();
		foreach($shopcart as $key=>$cgoods){	
			if($goodsKey != $key){
				$newShopcat[$key] = $cgoods;
			}			
		}
		session("cart",$newShopcat);
		return true;
		
	}
	

//修改购物车中的商品数量


	public function changeCartGoodsnum(){
		$goodsKey = (int)I("goodsId")."_".(int)I("goodsAttrId",0);
		$num = abs((int)I("num"));
		$ischk = (int)I("ischk",0);
		$shopcart = session("cart")?session("cart"):array();
		session("cart",null);
		$newShopcart = array();
		foreach($shopcart as $key=>$cgoods){	
			$cartgoods = $shopcart[$key];
			if($goodsKey == $key){
				$cartgoods["cnt"] = $num;
				$cartgoods["ischk"] = $ischk;
			}
			$newShopcart[$key] = $cartgoods;	
		}
		session("cart",$newShopcart);
		return true;
	}
	
//获取库存
	public function getGoodsStock($data){
	 	$goodsId = $data['goodsId'];
		$goodsAttrId = $data['goodsAttrId'];		
		$sql = "select goodsId,goodsStock,attrCatId from __PREFIX__goods where isSale=1 and goodsFlag=1 and goodsStatus=1 and goodsId=".$goodsId;		
	 	$goods = $this->query($sql);
	 	if($goods[0]['attrCatId']>0){
	 		$sql = "select ga.id,ga.attrStock from __PREFIX__goods_attributes ga where ga.goodsId=".$goodsId." and id=".$goodsAttrId;
			$priceAttrs = $this->query($sql);
			if(!empty($priceAttrs))$goods[0]['goodsStock'] = $priceAttrs[0]['attrStock'];
	 	}
	 	if(empty($goods))return array();
	 	return $goods[0];
	 }
}